package com.fairytask.ui.item.pet
{
	import com.fairytask.engine.components.BaseItem;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.managers.ResManager;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.resource.StaticIni;
	import com.greensock.TweenMax;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	/**
	 * 字格对象 
	 * @author xujh
	 * 
	 */
	public class WordItem extends BaseItem
	{
		private var _wordLab	:Clip	= new Clip();	//文字
		private var _bg		:Sprite	= new Sprite();
		
		private var _moneySp	:Sprite	= new Sprite();
		private var _moneyIcon	:Clip	= new Clip();	//仙玉图标
		private var _moneyLab	:Label	= new Label("00FF00");
		
		private var _word		:int;
		private var _pos		:int;		//位置
		private var _able		:Boolean	= true;	//状态
		private var _width		:int;
		private var _height	:int;
		//动画完成事件
		private var _func	:Function;
		private var _hasMoney	:Boolean	=	false;
		
		public function WordItem(val:int=0,w:int=65,h:int=65)
		{
			super();
			_width = w;
			_height = h;
			this.word = val;
			init();
		}
		
		/**
		 * 初始化 
		 * 
		 */
		private function init():void
		{
			//添加背景
			_bg = ResManager.newResClass("alpha") as MovieClip;
			_bg.width	= _width;
			_bg.height	= _height;
			/*_bg.graphics.beginFill(0xFFFFFF,0.5);	
			_bg.graphics.drawRect(0,0,_width,_height);
			_bg.graphics.endFill();*/
			addChild(_bg);
			//字位置设置
			/*_wordLab.x = _width*0.1;
			_wordLab.y = _height*0.1;*/
			addChild(_wordLab);
			
			//仙玉显示部分
			_moneySp.y	= 15;
			addChild(_moneySp);
			//金钱
			_moneyIcon.source = ResConfig.moneyIcon + "gold.png";
			_moneySp.addChild(_moneyIcon);
			_moneyLab.x	= 12;
			_moneySp.addChild(_moneyLab);
			_moneySp.visible = false;
		}
		
		/**
		 * 添加事件 
		 * 
		 */
		private function addEvent():void
		{
			this.addEventListener(MouseEvent.MOUSE_OVER,onOver);
			this.addEventListener(MouseEvent.MOUSE_OUT,onOut);
		}
		
		/**
		 * 删除事件 
		 * 
		 */
		private function delEvent():void
		{
			this.removeEventListener(MouseEvent.MOUSE_OVER,onOver);
			this.removeEventListener(MouseEvent.MOUSE_OUT,onOut);
		}
		
		/**
		 * 金钱tips 
		 * @param evt
		 * 
		 */
		private function onOver(evt:MouseEvent):void
		{
			TipsManage.showTips(StaticIni.getSwfLangStrVar("1000000559",[_moneyLab.htmlStrByBorder]));
		}
		
		private function onOut(evt:MouseEvent):void
		{
			ToolTips.hide();
		}
		
		/**
		 * 字动画 
		 * @param func
		 * 
		 */
		public function onFlash(func:Function=null):void
		{
			_func = func;
//			TweenLite.to(_wordLab,1,{alpha:0,onComplete:onFlashOver});
			TweenMax.to(_wordLab, 1, {glowFilter:{color:0xFFFFFF, alpha:1, blurX:30, blurY:30, strength:1, quality:1},repeat:2,yoyo:true,startAt:{glowFilter:{color:0xFFFFFF, alpha:0, blurX:0, blurY:0, strength:1, quality:1}},onComplete:onFlashOver});
		}
		
		/**
		 * 动画播放完成 
		 * 
		 */
		private function onFlashOver():void
		{
			//动画播放完，先判断是否可用
			if(_able)
			{
				_wordLab.filters = null;
			}
			else
			{
				_wordLab.filters = [Filter.GRAY];
			}
			if(null != _func)
			{
				_func();
			}
		}
		
		/**
		 * 设置字 
		 * @param val
		 * 
		 */
		public function set word(val:int):void
		{
			if(val == 0)
			{
				return;
			}
			_word = val;
			_wordLab.source = ResConfig.wordsPath + val + ".png";
			_wordLab.width = _width;
			_wordLab.height = _height;
//			_wordLab.width = _width * 0.8;
//			_wordLab.height = _height * 0.8;
		}
		
		public function get word():int
		{
			return _word;
		}
		
		/**
		 *  设置状态，0灰，1可用
		 * @param val
		 * 
		 */
		public function set able(val:Boolean):void
		{
			if(val)
			{
				_wordLab.filters = null;
			}
			else
			{
				_wordLab.filters = [Filter.GRAY];
			}
			_able = val;
		}
		
		public function get able():Boolean
		{
			return _able;
		}
		
		/**
		 * 设置位置 
		 * @param val
		 * 
		 */
		public function set pos(val:int):void
		{
			_pos = val;
		}
		
		public function get pos():int
		{
			return _pos;
		}
		
		/**
		 * 设置需要的钱 
		 * @param val
		 * 
		 */
		public function set money(val:int):void
		{
			_moneyLab.htmlStrByBorder = String(val);
			if(val == 0)
			{
				_hasMoney = false;
				_moneySp.visible = false;
				delEvent();
			}
			else
			{
				_moneySp.x = (_width - _moneyLab.width)/2 - 5;
				_hasMoney = true;
				_moneySp.visible = true;
				addEvent();
			}
		}
		
		/**
		 * 是否显示金钱 
		 * 
		 */
		public function set showMoney(val:Boolean):void
		{
			if(val)
			{
				if(_hasMoney)
				{
					_moneySp.visible = true;
					addEvent();
				}
			}
			else
			{
				_moneySp.visible = false;
				delEvent();
			}
		}
		
		/**
		 * 清除 
		 * 
		 */
		public function clearAll():void
		{
			_wordLab.clear();
			this.filters = null;
			_moneyLab.visible = false;
		}
	}
}